package com.tbit.uqbike.push.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.push.constant.GatewayProtocol;
import com.tbit.uqbike.push.constant.RabbitMQData;
import com.tbit.uqbike.push.constant.RedisConstant;
import com.tbit.uqbike.push.exception.BaseException;
import com.tbit.uqbike.push.pojo.Machine;
import com.tbit.uqbike.push.pojo.OrderLog;
import com.tbit.uqbike.push.pojo.dto.TerControlMsg;
import com.tbit.uqbike.push.pojo.dto.TerMsg;
import com.tbit.uqbike.push.service.*;
import com.tbit.uqbike.push.util.DateTimeUtil;
import com.tbit.uqbike.push.util.LogUtil;
import com.tbit.uqbike.push.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("terControlService")
public class TerControlServiceImpl implements TerControlService {
    @Autowired
    private MQProducer mQProducer;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private OrderLogService orderLogService;

    @Override
    public void sendControl(String serNO, String machineNO, String controlType, String paramName, String paramValue, String remark) {
        /**查询消息是在的队列名称*/
        String route_key = redisService.get(RedisUtil.getTerHashId(machineNO), RedisConstant.REDIS_TER_ROUTE_KEY);
        if (route_key != null && route_key.length() > 0) {
            /**封装消息*/
            TerMsg termsg = new TerMsg();
            termsg.setSn(machineNO);
            termsg.setSerNO(serNO);
            termsg.setControlType(controlType);
            termsg.setParamName(paramName);
            termsg.setParamValue(paramValue);

            TerControlMsg terControlMsg = new TerControlMsg();
            terControlMsg.setFeedback(RabbitMQData.queueName);
            terControlMsg.setMsgId(GatewayProtocol.MQ_MSG_ID_CONTROL_TER);
            terControlMsg.setData(termsg);

            /**发送到MQ*/
            mQProducer.sendDataToQueue(route_key, JSON.toJSONString(terControlMsg));

            /**添加到流水号到内存*/
            //SystemData.terControlMap.put(serNO, "-1");

            Machine machine = machineService.getByMachineNO(machineNO);
            if (machine != null) {
                OrderLog orderLog = new OrderLog();
                orderLog.setMachineId(machine.getMachineId());
                orderLog.setMachineNo(machine.getMachineNO());
                orderLog.setUserCode(machine.getUsercode());
                orderLog.setOpType(0);
                orderLog.setOpId(0);
                orderLog.setAccountUserId(0);
                orderLog.setOpTime(DateTimeUtil.getNowTime());
                orderLog.setChannelType(0);
                orderLog.setBusinessType(3);
                orderLog.setOrderType(controlType);
                orderLog.setOrderSerNo(serNO);
                orderLog.setOrderSource(9);
                orderLog.setOrderContent(paramName);
                orderLog.setRemark(remark);

                orderLogService.insert(orderLog);
            }
        } else {
            throw new BaseException("远程控制操作失败");
        }
    }

    @Override
    @Transactional
    public void respHandle(String serNO, String controlRet) {
        LogUtil.info("serNo:" + serNO + "回复" + controlRet);
    }

}  